package lesson4.example6;

import java.lang.reflect.Method;
import java.util.Arrays;
import net.bytebuddy.asm.*;

public class LoggerAdvisor {
	
	// 通过注解声明切点
    @Advice.OnMethodEnter
    // 上下文信息（例如当前对象，当前方法，当前参数等）可以通过Advice类内部的一系列注解声明，Byte Buddy会在运行时完成注入
    public static void onMethodEnter(@Advice.Origin Method method, @Advice.AllArguments Object[] arguments) {
        if (method.getAnnotation(Log.class) != null) {
            System.out.println("enter " + method.getName() + " with arguments: " + Arrays.toString(arguments));
        }
    }

    @Advice.OnMethodExit
    public static void onMethodExit(@Advice.Origin Method method, @Advice.AllArguments Object[] arguments, @Advice.Return Object ret) {
        if (method.getAnnotation(Log.class) != null) {
            System.out.println("exit " + method.getName() + " with arguments: " + Arrays.toString(arguments) + " return: " + ret);
        }
    }
}